package com.changgou.page.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.page.service.PageService;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@RestController
@CrossOrigin
public class PageController {


    @Autowired
    private PageService pageService;

    @GetMapping("/createPage")
    public void createPage(String spuId){
        pageService.createPage( spuId );
    }

    @GetMapping("/saveSkuId")
    public void saveSkuId(@RequestParam String skuId,@RequestParam String loginName){
        pageService.saveSkuId( skuId ,loginName);
    }

    @GetMapping("/getLoginName")
    public Result getLoginName(){
        OkHttpClient okHttpClient=new OkHttpClient();

        //请求对象
        Request request=new Request.Builder().url( "http://127.0.0.1:9102/user/loginName" ).build();

        Call call = okHttpClient.newCall( request );//执行对象
        //入队（执行）
        call.enqueue( new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("出错！");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("用户名为::::"+response.body().string());
            }
        } );
        return new Result(true,StatusCode.OK,"执行成功");
    }
}
